Cuestionario Ampliado del Censo de Población y Vivienda 2010
El cuestionario ampliado se guarda en un un archivo
_Base2020.RData.
data <- read_sav("~/Población 2010/Cuestionario Ampliado_2010_Persona.sav")
save(data,
file = paste0(here::here(),"/Bases/Censo_Personas_2010.RData"))Se seleccionan las variables que se desean conservar para la
realización de este documento y se guarda en un archivo
_Base2020.RData para practicidad del manejo de datos.
Posibles variables que se pueden contemplar en la migración reciente
EDADSEXOAFRODESHLENGUA QDIALECT_CPERETN NIVACADALFABETESTCON CONACT HIJOS_NAC_VIVOSload(paste0(here::here(),"/Bases/Censo_Personas_2010.RData"))
mydata <- data %>%
select(ENT, NOM_ENT, MUN, NOM_MUN, ENT_MUN,
LNACEDO_C, LNACPAIS_C,
RES05EDO_C, RES05PAI_C, MUN05OTR_C,
LTRABPAI_C, LTRABMUN_C, ENT_MUN_TRABAJO,
EDAD, SEXO, HLENGUA, QDIALECT_C, LI_INALI, PERETN, NIVACAD, ALFABET,
ESTCON, CONACT, OCUACTIV_C, HORTRA, INGTRMEN, SITTRA, FACTOR, ESTRATO, UPM) %>%
rename("CVE_ENT" = "ENT",
"CVE_MUN" = "ENT_MUN",
"MUN_TRAB" = "LTRABMUN_C",
"CVE_MUN_TRABAJO" = "ENT_MUN_TRABAJO") %>%
mutate(CVE_ENT = str_pad(.$CVE_ENT, width = 3, side = c("left"), pad = "0"),
CVE_MUN = str_pad(.$CVE_MUN, width = 6, side = c("left"), pad = "0")) %>%
mutate(CVE_MUN_RES = paste0(.$RES05EDO_C, MUN05OTR_C))Zonas Metropolitanas 2020
Se anexa la base de datos de las Zonas Metropolitanas 2020 a la base orginal
ZM_2020 <- read.xlsx(paste0(here::here(), "/Bases/Municipio/ZM_2020.xlsx"),
startRow = 7,
skipEmptyRows = TRUE) %>%
select(CVE_ZM, NOM_ZM, CVE_ENT, NOM_ENT, CVE_MUN, NOM_MUN, MC, CF) %>%
mutate(CVE_ENT = stringr::str_pad(.$CVE_ENT, width = 3, side = c("left"), pad = "0"),
CVE_MUN = stringr::str_pad(.$CVE_MUN, width = 6, side = c("left"), pad = "0"))Se asignan las claves de las zonas metropolitanas de acuerdo a las
diferentes variables de interes:
Residencia hace 5 años
Laboral
mydata <- mydata %>%
# Zonas Metropolitanas por residenicia
left_join(., ZM_2020 %>% select(-c(CVE_ENT, NOM_ENT, NOM_MUN)), by = c("CVE_MUN")) %>%
# Zonas Metropolitanas en el lugar de residencia hace 5 años
left_join(., ZM_2020 %>% select(-c(CVE_ENT, NOM_ENT, NOM_MUN)) %>%
rename("CVE_ZM_RES" = "CVE_ZM",
"ZM_RES" = "NOM_ZM"), by = c("CVE_MUN_RES" = "CVE_MUN")) %>%
# Zonas Metropolitanas en el lugar de trabajo
left_join(., ZM_2020 %>% select(-c(CVE_ENT, NOM_ENT, NOM_MUN)) %>%
rename("CVE_ZM_TRABAJO" = "CVE_ZM",
"ZM_TRABAJO" = "NOM_ZM"), by = c("CVE_MUN_TRABAJO" = "CVE_MUN"))
save(mydata, file = paste0(here::here(), "/Bases/06_Migracion por Zonas Metropolitanas_2010_Base2020.RData")) ✔️A partir de aquí se pueden correr los códidos 👇.
Se carga el archivo
Migracion por Zonas Metropolitanas_2010_Base2020.RData.
load(file = paste0(here::here(), "/Bases/06_Migracion por Zonas Metropolitanas_2010_Base2020.RData"))
# Para fines prácticos se genera un ponderador de uno
mydata <- mydata %>%
select(CVE_ENT, NOM_ENT, CVE_MUN, NOM_MUN, RES05EDO_C, CVE_MUN_RES,
CVE_ZM, NOM_ZM, CVE_ZM_RES, ZM_RES,
EDAD, FACTOR, ESTRATO, UPM) %>%
mutate(M = 1)
# Se vuelve a cargar la base de datos para fines practicos
ZM_2020 <- read.xlsx(paste0(here::here(), "/Bases/Municipio/ZM_2020.xlsx"),
startRow = 7,
skipEmptyRows = TRUE) %>%
select(CVE_ZM, NOM_ZM, CVE_ENT, NOM_ENT, CVE_MUN, NOM_MUN, MC, CF) %>%
mutate(CVE_ENT = stringr::str_pad(.$CVE_ENT, width = 3, side = c("left"), pad = "0"),
CVE_MUN = stringr::str_pad(.$CVE_MUN, width = 6, side = c("left"), pad = "0"))Entidades y Municipios
Se genera un vector con el nombre de las entidades llamado
estados para facilitar los filtros en el documento.
Se genera un vector con las abreviaturas de las entidades llamado
ent para fines prácticos.
Se genera un vector con las claves de los municipios.
# Claves de los estados
estados <- sjlabelled::get_labels(mydata$CVE_ENT)
nom_estados <- c( "Aguascalientes", "Baja California" ,"Baja California Sur", "Campeche", "Coahuila de Zaragoza", "Colima",
"Chiapas", "Chihuahua", "Ciudad de México", "Durango", "Guanajuato", "Guerrero", "Hidalgo", "Jalisco",
"México", "Michoacán de Ocampo", "Morelos", "Nayarit", "Nuevo León", "Oaxaca", "Puebla", "Querétaro",
"Quintana Roo", "San Luis Potosí", "Sinaloa", "Sonora", "Tabasco", "Tamaulipas", "Tlaxcala",
"Veracruz de Ignacio de la Llave", "Yucatán", "Zacatecas")
est <- c("AGS", "BC", "BCS", "CAMP", "COAH", "COL", "CHIS", "CHIH", "CDMX", "DGO", "GTO", "GRO", "HGO",
"JAL", "MEX", "MICH", "MOR", "NAY", "NL", "OAX", "PUE", "QRO", "QROO", "SLP","SIN","SON", "TAB",
"TAMS", "TLX", "VER", "YUC", "ZAC")
# Claves de los municipios
MUN <- readRDS(paste0(here::here(), "/Bases/municipios_2010.RDS"))
nom_municipios <- sjlabelled::get_labels(MUN$NOM_MUN) %>% as.factor()
municipios <- sjlabelled::get_labels(MUN$CVE_MUN) %>% as.factor()
#saveRDS(MUN, file = paste0(here::here(), "/Bases/municipios_2010_Base2020.RDS"))
# Claves de las zonas metropolitanas
zm <- sjlabelled::get_labels(mydata$CVE_ZM)[-2]
nom_zm <- sjlabelled::get_labels(mydata$NOM_ZM)[-2]Se utiliza la paquetería survey para poder trabajar con
la muestra del cuestionario ampliado, en la cual se selecciona a la
población de 5 años y más.
options(survey.lonely.psu = "adjust")
MC <- mydata %>%
select(CVE_ENT, CVE_MUN, RES05EDO_C, CVE_MUN_RES, CVE_ZM, CVE_ZM_RES, M, EDAD, FACTOR, ESTRATO, UPM) %>%
# Se genera una indicadora de zm
mutate(I_ZM_2020 = ifelse(is.na(.$CVE_ZM), '0', '1'),
I_RES5A_ZM_2020 = ifelse(is.na(.$CVE_ZM_RES), '0', '1')) %>%
# Se clasifican a los migrantes internos
mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_RES ~ 'Pertenecen a la Zona Metropolitana', #Residentes
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM == .$CVE_ZM_RES ~ "Pertenecen a la Zona Metropolitana", #Migrantes que residen en la misma zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM != .$CVE_ZM_RES ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residen en otra zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian fuera de la zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian dentro de una zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Migrantes que no residen en la zona metropolitana
)) %>%
filter(EDAD >= 5 & EDAD <= 130) %>%
filter(CVE_MUN_RES %in% municipios) %>%
svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)
saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_municipal_Base2020.RDS"))MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_municipal_Base2020.RDS"))
Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC) Se genera la matriz cuadrada y se le asignan los nombres de los estados.
Migrantes <- Migrantes %>%
as.data.frame() %>%
expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
as.data.frame() %>%
rename("CVE_MUN" = "row_labels") %>%
arrange(CVE_MUN) %>%
slice(-1)
rownames <- Migrantes %>%
mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>%
pull(CVE_MUN)
colnames <- names(Migrantes) %>%
as.data.frame() %>%
slice(-1) %>%
rename("CVE_MUN" = ".") %>%
mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
pull(CVE_MUN)
# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
select(-CVE_MUN)
rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames
saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel municipal_Base2020.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel municipal_Base2020.RData"))
require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Reciente")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel municipal_Base2020.xlsx"), overwrite = TRUE)Matriz de migración reciente hace 5 años a nivel municipal, 2005 - 2010
| Matriz de migración reciente 2005 - 2010 | |||||||||||||||||||||||||||||
| Nivel municipal | |||||||||||||||||||||||||||||
| CVE_MUN | 001001 | 001002 | 001003 | 001004 | 001005 | 001006 | 001007 | 001008 | 001009 | 001010 | 001011 | 002001 | 002002 | 002003 | 002004 | 002005 | 003001 | 003002 | 003003 | 003008 | 003009 | 004001 | 004002 | 004003 | 004004 | 004005 | 004006 | 004007 | 004008 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||||||||||||||||||||
Se realizan cálculos generales de migración:
Residentes
Inmigrantes
Emigrantes
% Inmigrantes
% Emigrante
Migración bruta
Migración Neta
% Tasa de migración bruta
% Tasa de migración neta
Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.
################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
as.data.frame() %>%
group_by(CVE_MUN) %>%
summarise(Pob.Total = sum(FACTOR))
################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
as.data.frame() %>%
mutate(EDAD = as.numeric(.$EDAD)) %>%
subset(EDAD >= 5 & EDAD <=130) %>%
group_by(CVE_MUN) %>%
summarise(Pob.5ymas = sum(FACTOR))
################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel municipal_Base2020.RData"))
Residentes <- Migrantes %>%
rownames_to_column() %>%
gather(CVE_MUN, Value, -rowname)%>%
filter(rowname == CVE_MUN) %>%
select(-rowname) %>%
droplevels() %>%
rename("Residentes" = "Value")
################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
as_tibble() %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
group_by(CVE_MUN) %>%
summarise(Inmigrantes = sum(value, na.rm = TRUE))
################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
as_tibble() %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
group_by(CVE_MUN_RES) %>%
summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
rename("CVE_MUN" = "CVE_MUN_RES")
tabla <- Pob.Total %>%
left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
left_join(., Residentes, by = c("CVE_MUN")) %>%
left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
left_join(., Emigrantes, by = c("CVE_MUN")) %>%
mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
Mig.Bruta = .$Inmigrantes + .$Emigrantes,
Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Mig = Tasa.Inmig - Tasa.Emig,
Eficacia = Mig.Neta - Mig.Bruta)
write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010_Base2020.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010_Base2020.RData"))| Indicadores de migración reciente a nivel municipal | |||||||||||
| Zonas Metropolitanas | |||||||||||
| CVE_MUN | Pob.Total | Pob.5ymas | Residentes | Inmigrantes | Emigrantes | Mig.Neta | Mig.Bruta | Tasa.Inmig | Tasa.Emig | Tasa.Mig | Eficacia |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||
Se genera la matriz cuadrada y se le asignan los nombres de los estados.
Se utiliza la paquetería survey para poder trabajar con
la muestra del cuestionario ampliado, en la cual se selecciona a la
población de 5 años y más.
options(survey.lonely.psu = "adjust")
MC <- mydata %>%
select(CVE_ENT, CVE_MUN, RES05EDO_C, CVE_MUN_RES, CVE_ZM, CVE_ZM_RES, M, EDAD, FACTOR, ESTRATO, UPM) %>%
# Se genera una indicadora de zm
mutate(I_ZM_2020 = ifelse(is.na(.$CVE_ZM), '0', '1'),
I_RES5A_ZM_2020 = ifelse(is.na(.$CVE_ZM_RES), '0', '1')) %>%
# Se clasifican a los migrantes internos
mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_RES ~ 'Pertenecen a la Zona Metropolitana', #Residentes
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM == .$CVE_ZM_RES ~ "Pertenecen a la Zona Metropolitana", #Migrantes que residen en la misma zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM != .$CVE_ZM_RES ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residen en otra zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian fuera de la zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian dentro de una zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Migrantes que no residen en la zona metropolitana
)) %>%
filter(EDAD >= 5 & EDAD <= 130) %>%
filter(CVE_MUN_RES %in% municipios & .$I_ZM %in% "Pertenecen a la Zona Metropolitana") %>%
svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)
saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_intramunicipal_Base2020.RDS"))Se genera una matriz cruzada del lugar de residencia hace 5 años a
nivel municipal, utilizando la función svytable de la
paquetería survey.
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_intramunicipal_Base2020.RDS"))
Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC)Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.
Migrantes <- Migrantes %>%
as.data.frame() %>%
expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
as.data.frame() %>%
rename("CVE_MUN" = "row_labels") %>%
arrange(CVE_MUN) %>%
slice(-1)
rownames <- Migrantes %>%
mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>%
pull(CVE_MUN)
colnames <- names(Migrantes) %>%
as.data.frame() %>%
slice(-1) %>%
rename("CVE_MUN" = ".") %>%
mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
pull(CVE_MUN)
# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
select(-CVE_MUN)
rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames
saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel intramunicipal 2010_Base2020.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel intramunicipal 2010_Base2020.RData"))
require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intramunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel intramunicipal 2010_Base2020.xlsx"), overwrite = TRUE)Matriz de migración reciente hace 5 años a nivel municipal, 2005 - 2010
| Matriz de migración reciente por zonas metropolitanas | |||||||||||||||||||||||||||||
| Nivel intramunicipal | |||||||||||||||||||||||||||||
| CVE_MUN | 001001 | 001002 | 001003 | 001004 | 001005 | 001006 | 001007 | 001008 | 001009 | 001010 | 001011 | 002001 | 002002 | 002003 | 002004 | 002005 | 003001 | 003002 | 003003 | 003008 | 003009 | 004001 | 004002 | 004003 | 004004 | 004005 | 004006 | 004007 | 004008 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||||||||||||||||||||
MR <- NULL
for(i in 1:length(zm)){
tabla <- ZM_2020 %>%
filter(CVE_MUN %in% municipios) %>%
select(CVE_ZM, CVE_MUN) %>%
filter(CVE_ZM %in% zm[i]) %>%
pull(CVE_MUN)
MR[[paste0(zm[i])]] <- Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
mutate_if(is.numeric, as.numeric) %>%
select(CVE_MUN, all_of(tabla)) %>%
filter(CVE_MUN %in% tabla)
}
# Se guardan en un objeto de R
saveRDS(MR, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matrices de MR5a a nivel intramunicipal por ZM2010_Base2020.RDS"))
# Se genera un Excel con todas las matrices por ZM
wb <- createWorkbook()
for(i in 1:length(zm)){
addWorksheet(wb, paste0(zm[i]))
writeData(wb, i, MR[[paste0(zm[i])]] %>% as.data.frame())
saveWorkbook(wb,
file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matrices de MR5a a nivel intramunicipal por ZM2010_Base2020.xlsx"),
overwrite = TRUE)
}Matriz de migración reciente hace 5 años en la Zona Metropolitana de Cuernavaca, 2005 - 2010
| Matriz de migración reciente a nivel intramunicipal | ||||
| Zona Metropolitana de Cuernavaca | ||||
| CVE_MUN | 017002 | 017004 | 017006 | 017030 |
|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | ||||
Se realizan cálculos generales de migración:
Residentes
Inmigrantes
Emigrantes
% Inmigrantes
% Emigrante
Migración bruta
Migración Neta
% Tasa de migración bruta
% Tasa de migración neta
Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.
################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
as.data.frame() %>%
group_by(CVE_MUN) %>%
summarise(Pob.Total = sum(FACTOR))
################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
as.data.frame() %>%
mutate(EDAD = as.numeric(.$EDAD)) %>%
subset(EDAD >= 5 & EDAD <=130) %>%
group_by(CVE_MUN) %>%
summarise(Pob.5ymas = sum(FACTOR))
################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel intramunicipal 2010_Base2020.RData"))
Residentes <- Migrantes %>%
rownames_to_column() %>%
gather(CVE_MUN, Value, -rowname)%>%
filter(rowname == CVE_MUN) %>%
select(-rowname) %>%
droplevels() %>%
rename("Residentes" = "Value")
################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
as_tibble() %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
group_by(CVE_MUN) %>%
summarise(Inmigrantes = sum(value, na.rm = TRUE))
################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
as_tibble() %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
group_by(CVE_MUN_RES) %>%
summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
rename("CVE_MUN" = "CVE_MUN_RES")
tabla <- Pob.Total %>%
left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
left_join(., Residentes, by = c("CVE_MUN")) %>%
left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
left_join(., Emigrantes, by = c("CVE_MUN")) %>%
mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
Mig.Bruta = .$Inmigrantes + .$Emigrantes,
Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Mig = Tasa.Inmig - Tasa.Emig,
Eficacia = Mig.Neta - Mig.Bruta)
write.xlsx(tabla,
file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Intramunicipal)_Base2020.xlsx"),
overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Intramunicipal)_Base2020.RData"))| Indicadores de migración reciente a nivel intramunicipal | |||||||||||
| Zonas Metropolitanas | |||||||||||
| CVE_MUN | Pob.Total | Pob.5ymas | Residentes | Inmigrantes | Emigrantes | Mig.Neta | Mig.Bruta | Tasa.Inmig | Tasa.Emig | Tasa.Mig | Eficacia |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||
options(survey.lonely.psu = "adjust")
MC <- mydata %>%
select(CVE_ENT, CVE_MUN, RES05EDO_C, CVE_MUN_RES, CVE_ZM, CVE_ZM_RES, M, EDAD, FACTOR, ESTRATO, UPM) %>%
# Se genera una indicadora de zm
mutate(I_ZM_2020 = ifelse(is.na(.$CVE_ZM), '0', '1'),
I_RES5A_ZM_2020 = ifelse(is.na(.$CVE_ZM_RES), '0', '1')) %>%
# Se clasifican a los migrantes internos
mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_RES ~ 'Pertenecen a la Zona Metropolitana', #Residentes
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM == .$CVE_ZM_RES ~ "Pertenecen a la Zona Metropolitana", #Migrantes que residen en la misma zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM != .$CVE_ZM_RES ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residen en otra zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian fuera de la zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian dentro de una zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Migrantes que no residen en la zona metropolitana
)) %>%
filter(EDAD >= 5 & EDAD <= 130) %>%
filter(CVE_MUN_RES %in% municipios & .$I_ZM %in% "No pertenecen a la Zona Metropolitana") %>%
svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)
saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_intermunicipal_Base2020.RDS"))Se genera una matriz cruzada del lugar de residencia hace 5 años a
nivel municipal, utilizando la función svytable de la
paquetería survey.
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_intermunicipal_Base2020.RDS"))
Migrantes <- svytable(~CVE_MUN_RES + CVE_MUN, design = MC)Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.
Migrantes <- Migrantes %>%
as.data.frame() %>%
expss::cross_cases(CVE_MUN, CVE_MUN_RES, weight = Freq) %>%
as.data.frame() %>%
rename("CVE_MUN" = "row_labels") %>%
arrange(CVE_MUN) %>%
slice(-1)
rownames <- Migrantes %>%
mutate(CVE_MUN = substr(.$CVE_MUN, 9, 16)) %>%
pull(CVE_MUN)
colnames <- names(Migrantes) %>%
as.data.frame() %>%
slice(-1) %>%
rename("CVE_MUN" = ".") %>%
mutate(`CVE_MUN` = substr(.$CVE_MUN, 13, 20)) %>%
pull(CVE_MUN)
# Se elimina la variable CVE_MUN
Migrantes <- Migrantes %>%
select(-CVE_MUN)
rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames
saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel intermunicipal 2010_Base2020.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel intermunicipal 2010_Base2020.RData"))
require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Intermunicipal")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_MUN"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel intermunicipal 2010_Base2020.xlsx"), overwrite = TRUE)Matriz de migración reciente hace 5 años a nivel intermunicipal, 2005 - 2010
| Matriz de migración reciente por zonas metropolitanas | |||||||||||||||||||||||||||||
| Nivel intermunicipal | |||||||||||||||||||||||||||||
| CVE_MUN | 001001 | 001002 | 001003 | 001004 | 001005 | 001006 | 001007 | 001008 | 001009 | 001010 | 001011 | 002001 | 002002 | 002003 | 002004 | 002005 | 003001 | 003002 | 003003 | 003008 | 003009 | 004001 | 004002 | 004003 | 004004 | 004005 | 004006 | 004007 | 004008 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||||||||||||||||||||
Se realizan cálculos generales de migración:
Residentes
Inmigrantes
Emigrantes
% Inmigrantes
% Emigrante
Migración bruta
Migración Neta
% Tasa de migración bruta
% Tasa de migración neta
Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.
################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
as.data.frame() %>%
group_by(CVE_MUN) %>%
summarise(Pob.Total = sum(FACTOR))
################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
as.data.frame() %>%
mutate(EDAD = as.numeric(.$EDAD)) %>%
subset(EDAD >= 5 & EDAD <= 130) %>%
group_by(CVE_MUN) %>%
summarise(Pob.5ymas = sum(FACTOR))
################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel intermunicipal 2010_Base2020.RData"))
Residentes <- Migrantes %>%
rownames_to_column() %>%
gather(CVE_MUN, Value, -rowname)%>%
filter(rowname == CVE_MUN) %>%
select(-rowname) %>%
droplevels() %>%
rename("Residentes" = "Value")
################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
as_tibble() %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
group_by(CVE_MUN) %>%
summarise(Inmigrantes = sum(value, na.rm = TRUE))
################################################################################
############################### Emigrantes #####################################
Emigrantes <- Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
as_tibble() %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
group_by(CVE_MUN_RES) %>%
summarise(Emigrantes = sum(value, na.rm = TRUE)) %>%
rename("CVE_MUN" = "CVE_MUN_RES")
tabla <- Pob.Total %>%
left_join(., Pob.5ymas, by = c("CVE_MUN")) %>%
left_join(., Residentes, by = c("CVE_MUN")) %>%
left_join(., Inmigrantes, by = c("CVE_MUN")) %>%
left_join(., Emigrantes, by = c("CVE_MUN")) %>%
mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
Mig.Bruta = .$Inmigrantes + .$Emigrantes,
Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Mig = Tasa.Inmig - Tasa.Emig,
Eficacia = Mig.Neta - Mig.Bruta)
write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Intermunicipal)_Base2020.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Intermunicipal)_Base2020.RData"))| Indicadores de migración reciente a nivel intermunicipal | |||||||||||
| Zonas Metropolitanas | |||||||||||
| CVE_MUN | Pob.Total | Pob.5ymas | Residentes | Inmigrantes | Emigrantes | Mig.Neta | Mig.Bruta | Tasa.Inmig | Tasa.Emig | Tasa.Mig | Eficacia |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||
Se utiliza la paquetería survey para poder trabajar con
la muestra del cuestionario ampliado, en la cual se selecciona a la
población de 5 años y más.
options(survey.lonely.psu = "adjust")
MC <- mydata %>%
select(CVE_ENT, CVE_MUN, RES05EDO_C, CVE_MUN_RES, CVE_ZM, CVE_ZM_RES, M, EDAD, FACTOR, ESTRATO, UPM) %>%
# Se genera una indicadora de zm
mutate(I_ZM_2020 = ifelse(is.na(.$CVE_ZM), '0', '1'),
I_RES5A_ZM_2020 = ifelse(is.na(.$CVE_ZM_RES), '0', '1')) %>%
# Se clasifican a los migrantes internos
mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_RES ~ 'Pertenecen a la Zona Metropolitana', #Residentes
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM == .$CVE_ZM_RES ~ "Pertenecen a la Zona Metropolitana", #Migrantes que residen en la misma zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM != .$CVE_ZM_RES ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residen en otra zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian fuera de la zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian dentro de una zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Migrantes que no residen en la zona metropolitana
)) %>%
filter(EDAD >= 5 & EDAD <= 130) %>%
filter(CVE_MUN_RES %in% municipios) %>%
svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)
saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_metropolitana_Base2020.RDS"))Se genera una matriz cruzada del lugar de residencia hace 5 años a
nivel municipal, utilizando la función svytable de la
paquetería survey.
MC <- readRDS(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_metropolitana_Base2020.RDS"))
Migrantes <- svytable(~CVE_ZM_RES + CVE_ZM, design = MC)Se genera la matriz cuadrada y se le asignan las etiquetas de municipios.
Migrantes <- Migrantes %>%
as.data.frame() %>%
expss::cross_cases(CVE_ZM, CVE_ZM_RES, weight = Freq) %>%
as.data.frame() %>%
rename("CVE_ZM" = "row_labels") %>%
arrange(CVE_ZM) %>%
slice(-1)
rownames <- Migrantes %>%
mutate(CVE_ZM = substr(.$CVE_ZM, 8, 12)) %>%
pull(CVE_ZM)
colnames <- names(Migrantes) %>%
as.data.frame() %>%
slice(-1) %>%
rename("CVE_ZM" = ".") %>%
mutate(`CVE_ZM` = substr(.$CVE_ZM, 12, 16)) %>%
pull(CVE_ZM)
# Se elimina la variable CVE_ZM
Migrantes <- Migrantes %>%
select(-CVE_ZM)
rownames(Migrantes) <- rownames
colnames(Migrantes) <- colnames
saveRDS(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de MR5a a nivel metropolitano 2010_Base2020.RDS"))
save(Migrantes, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de MR5a a nivel metropolitano 2010_Base2020.RData"))
require(openxlsx)
wb <- createWorkbook()
addWorksheet(wb, "M.Metropolitano")
writeData(wb, 1, Migrantes %>% as.data.frame() %>% tibble::rownames_to_column(var = "CVE_ZM"), colNames = TRUE)
saveWorkbook(wb, file = paste0(here::here(), "/Bases/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de MR5a a nivel metropolitano 2010_Base2020.xlsx"), overwrite = TRUE)Matriz de migración reciente hace 5 años a nivel municipal, 2005 - 2010
| Matriz de migración reciente por zonas metropolitanas | |||||||||||||||||||||||||||||
| Zonas metropolitanas | |||||||||||||||||||||||||||||
| CVE_ZM | 01.01 | 02.01 | 02.02 | 02.03 | 03.01 | 03.02 | 04.01 | 05.01 | 05.02 | 05.03 | 05.04 | 05.05 | 06.01 | 06.02 | 07.01 | 07.02 | 08.01 | 08.02 | 08.03 | 08.04 | 09.01 | 10.01 | 11.01 | 11.02 | 11.03 | 11.04 | 11.06 | 11.07 | 12.01 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||||||||||||||||||||
Se realizan cálculos generales de migración:
Residentes
Inmigrantes
Emigrantes
% Inmigrantes
% Emigrante
Migración bruta
Migración Neta
% Tasa de migración bruta
% Tasa de migración neta
Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.
################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
as.data.frame() %>%
group_by(CVE_ZM) %>%
summarise(Pob.Total = sum(FACTOR))
################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
as.data.frame() %>%
mutate(EDAD = as.numeric(.$EDAD)) %>%
subset(EDAD >= 5 & EDAD <= 130) %>%
group_by(CVE_ZM) %>%
summarise(Pob.5ymas = sum(FACTOR))
################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel municipal_Base2020.RData"))
ZM <- lapply(1:length(zm), function(x){
ZM_2020 %>%
select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
filter(CVE_ZM %in% zm[x]) %>%
pull(CVE_MUN)
})
Residentes <- lapply(1:length(zm), function(x){
Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
filter(CVE_MUN == CVE_MUN_RES) %>%
filter(CVE_MUN %in% ZM[[x]]) %>%
summarize(Residentes = sum(value, na.rm = TRUE)) %>%
mutate(CVE_ZM = !!paste0(zm[x]))
})
Residentes <- do.call(rbind.data.frame, Residentes)
################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- lapply(1:length(zm), function(x){
Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
filter(CVE_MUN %in% ZM[[x]]) %>%
summarize(Inmigrantes = sum(value, na.rm = TRUE)) %>%
mutate(CVE_ZM = !!paste0(zm[x]))
})
Inmigrantes <- do.call(rbind.data.frame, Inmigrantes)
################################################################################
############################### Emigrantes #####################################
Emigrantes <- lapply(1:length(zm), function(x){
Migrantes %>%
t() %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
filter(CVE_MUN %in% ZM[[x]]) %>%
summarize(Emigrantes = sum(value, na.rm = TRUE)) %>%
mutate(CVE_ZM = !!paste0(zm[x]))
})
Emigrantes <- do.call(rbind.data.frame, Emigrantes)
tabla <- Pob.Total %>%
left_join(., Pob.5ymas, by = c("CVE_ZM")) %>%
left_join(., Residentes, by = c("CVE_ZM")) %>%
left_join(., Inmigrantes, by = c("CVE_ZM")) %>%
left_join(., Emigrantes, by = c("CVE_ZM")) %>%
mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
Mig.Bruta = .$Inmigrantes + .$Emigrantes,
Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Mig = Tasa.Inmig - Tasa.Emig,
Eficacia = Mig.Neta - Mig.Bruta)
write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Metropolitano) 2010_Base2020.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Metropolitano) 2010_Base2020.RData"))| Indicadores de migración reciente (Nivel metropolitano) | |||||||||||
| Zonas Metropolitanas | |||||||||||
| CVE_ZM | Pob.Total | Pob.5ymas | Residentes | Inmigrantes | Emigrantes | Mig.Neta | Mig.Bruta | Tasa.Inmig | Tasa.Emig | Tasa.Mig | Eficacia |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||
Se utiliza la paquetería survey para poder trabajar con
la muestra del cuestionario ampliado, en la cual se selecciona a la
población de 5 años y más.
options(survey.lonely.psu = "adjust")
MC <- mydata %>%
select(CVE_ENT, CVE_MUN, RES05EDO_C, CVE_MUN_RES, CVE_ZM, CVE_ZM_RES, M, EDAD, FACTOR, ESTRATO, UPM) %>%
# Se genera una indicadora de zm
mutate(I_ZM_2020 = ifelse(is.na(.$CVE_ZM), '0', '1'),
I_RES5A_ZM_2020 = ifelse(is.na(.$CVE_ZM_RES), '0', '1')) %>%
# Se clasifican a los migrantes internos
mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_RES ~ 'Pertenecen a la Zona Metropolitana', #Residentes
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM == .$CVE_ZM_RES ~ "Pertenecen a la Zona Metropolitana", #Migrantes que residen en la misma zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM != .$CVE_ZM_RES ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residen en otra zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian fuera de la zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian dentro de una zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Migrantes que no residen en la zona metropolitana
)) %>%
filter(EDAD >= 5 & EDAD <= 130) %>%
filter(CVE_MUN_RES %in% municipios & .$I_ZM %in% "Pertenecen a la Zona Metropolitana") %>%
svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)
saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_intrametropolitana_Base2020.RDS"))Se realizan cálculos generales de migración:
Residentes
Inmigrantes
Emigrantes
% Inmigrantes
% Emigrante
Migración bruta
Migración Neta
% Tasa de migración bruta
% Tasa de migración neta
Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.
################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
as.data.frame() %>%
group_by(CVE_ZM) %>%
summarise(Pob.Total = sum(FACTOR))
################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
as.data.frame() %>%
mutate(EDAD = as.numeric(.$EDAD)) %>%
subset(EDAD >= 5 & EDAD <=130) %>%
group_by(CVE_ZM) %>%
summarise(Pob.5ymas = sum(FACTOR))
################################################################################
########################### Residentes #########################################
MR <- readRDS(paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matrices de MR5a a nivel intramunicipal por ZM2010_Base2020.RDS"))
Residentes <- lapply(1:length(zm), function(x){
MR[[x]] %>%
as.data.frame() %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
filter(CVE_MUN == CVE_MUN_RES) %>%
summarize(Residentes = sum(value, na.rm = TRUE)) %>%
mutate(CVE_ZM = !!paste0(zm[x]))
})
Residentes <- do.call(rbind.data.frame, Residentes)
################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- lapply(1:length(zm), function(x){
MR[[x]] %>%
as.data.frame() %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
summarize(Inmigrantes = sum(value, na.rm = TRUE)) %>%
mutate(CVE_ZM = !!paste0(zm[x]))
})
Inmigrantes <- do.call(rbind.data.frame, Inmigrantes)
################################################################################
############################### Emigrantes #####################################
Emigrantes <- lapply(1:length(zm), function(x){
MR[[x]] %>%
tibble::column_to_rownames(var = "CVE_MUN") %>%
t() %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
summarize(Emigrantes= sum(value, na.rm = TRUE)) %>%
mutate(CVE_ZM = !!paste0(zm[x]))
})
Emigrantes <- do.call(rbind.data.frame, Emigrantes)
tabla <- Pob.Total %>%
left_join(., Pob.5ymas, by = c("CVE_ZM")) %>%
left_join(., Residentes, by = c("CVE_ZM")) %>%
left_join(., Inmigrantes, by = c("CVE_ZM")) %>%
left_join(., Emigrantes, by = c("CVE_ZM")) %>%
mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
Mig.Bruta = .$Inmigrantes + .$Emigrantes,
Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Mig = Tasa.Inmig - Tasa.Emig,
Eficacia = Mig.Neta - Mig.Bruta)
write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Intrametropolitano) 2010_Base2020.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Intrametropolitano) 2010_Base2020.RData"))| Indicadores de migración reciente (Nivel intrametropolitano) | |||||||||||
| Zonas Metropolitanas | |||||||||||
| CVE_ZM | Pob.Total | Pob.5ymas | Residentes | Inmigrantes | Emigrantes | Mig.Neta | Mig.Bruta | Tasa.Inmig | Tasa.Emig | Tasa.Mig | Eficacia |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||
Se utiliza la paquetería survey para poder trabajar con
la muestra del cuestionario ampliado, en la cual se selecciona a la
población de 5 años y más.
options(survey.lonely.psu = "adjust")
MC <- mydata %>%
select(CVE_ENT, CVE_MUN, RES05EDO_C, CVE_MUN_RES, CVE_ZM, CVE_ZM_RES, M, EDAD, FACTOR, ESTRATO, UPM) %>%
# Se genera una indicadora de zm
mutate(I_ZM_2020 = ifelse(is.na(.$CVE_ZM), '0', '1'),
I_RES5A_ZM_2020 = ifelse(is.na(.$CVE_ZM_RES), '0', '1')) %>%
# Se clasifican a los migrantes internos
mutate(I_ZM = case_when(.$CVE_MUN == .$CVE_MUN_RES ~ 'Pertenecen a la Zona Metropolitana', #Residentes
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM == .$CVE_ZM_RES ~ "Pertenecen a la Zona Metropolitana", #Migrantes que residen en la misma zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '1' & .$CVE_ZM != .$CVE_ZM_RES ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residen en otra zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '1' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian fuera de la zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '1' ~ 'No pertenecen a la Zona Metropolitana', #Migrantes que residian dentro de una zona metropolitana
.$CVE_MUN != .$CVE_MUN_RES & .$I_ZM_2020 %in% '0' & .$I_RES5A_ZM_2020 %in% '0' ~ 'No pertenecen a la Zona Metropolitana' #Migrantes que no residen en la zona metropolitana
)) %>%
filter(EDAD >= 5 & EDAD <= 130) %>%
filter(CVE_MUN_RES %in% municipios & .$I_ZM %in% "No pertenecen a la Zona Metropolitana") %>%
svydesign(data = ., id = ~ UPM, strata = ~ESTRATO, weight = ~FACTOR, nest = T)
saveRDS(MC, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/MC_intermetropolitana_Base2020.RDS"))Se realizan cálculos generales de migración:
Residentes
Inmigrantes
Emigrantes
% Inmigrantes
% Emigrante
Migración bruta
Migración Neta
% Tasa de migración bruta
% Tasa de migración neta
Se trabaja con la matriz cuadrada, la cual de esta manera no se satura la computadora.
################################################################################
############################ Población total ###################################
Pob.Total <- mydata %>%
as.data.frame() %>%
group_by(CVE_ZM) %>%
summarise(Pob.Total = sum(FACTOR))
################################################################################
###################### Población de 5 años y más ###############################
Pob.5ymas <- mydata %>%
as.data.frame() %>%
mutate(EDAD = as.numeric(.$EDAD)) %>%
subset(EDAD >= 5 & EDAD <=130) %>%
group_by(CVE_ZM) %>%
summarise(Pob.5ymas = sum(FACTOR))
################################################################################
########################### Residentes #########################################
load(file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Matriz de migracion reciente a nivel intermunicipal 2010_Base2020.RData"))
ZM <- lapply(1:length(zm), function(x){
ZM_2020 %>%
select(CVE_ZM, CVE_MUN, NOM_MUN) %>%
filter(CVE_ZM %in% zm[x]) %>%
pull(CVE_MUN)
})
Residentes <- lapply(1:length(zm), function(x){
Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
filter(CVE_MUN == CVE_MUN_RES) %>%
filter(CVE_MUN %in% ZM[[x]]) %>%
summarize(Residentes = sum(value, na.rm = TRUE)) %>%
mutate(CVE_ZM = !!paste0(zm[x]))
})
Residentes <- do.call(rbind.data.frame, Residentes)
################################################################################
############################### Inmigrantes ####################################
Inmigrantes <- lapply(1:length(zm), function(x){
Migrantes %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
filter(CVE_MUN %in% ZM[[x]]) %>%
summarize(Inmigrantes = sum(value, na.rm = TRUE)) %>%
mutate(CVE_ZM = !!paste0(zm[x]))
})
Inmigrantes <- do.call(rbind.data.frame, Inmigrantes)
################################################################################
############################### Emigrantes #####################################
Emigrantes <- lapply(1:length(zm), function(x){
Migrantes %>%
t() %>%
as.data.frame() %>%
tibble::rownames_to_column(var = "CVE_MUN") %>%
melt(., id.vars = "CVE_MUN", variable.name = "CVE_MUN_RES") %>%
mutate_at(vars(3), as.numeric) %>%
filter(CVE_MUN != CVE_MUN_RES) %>%
filter(CVE_MUN %in% ZM[[x]]) %>%
summarize(Emigrantes = sum(value, na.rm = TRUE)) %>%
mutate(CVE_ZM = !!paste0(zm[x]))
})
Emigrantes <- do.call(rbind.data.frame, Emigrantes)
tabla <- Pob.Total %>%
left_join(., Pob.5ymas, by = c("CVE_ZM")) %>%
left_join(., Residentes, by = c("CVE_ZM")) %>%
left_join(., Inmigrantes, by = c("CVE_ZM")) %>%
left_join(., Emigrantes, by = c("CVE_ZM")) %>%
mutate(Mig.Neta = .$Inmigrantes - .$Emigrantes,
Mig.Bruta = .$Inmigrantes + .$Emigrantes,
Tasa.Inmig = ((.$Inmigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Emig = ((.$Emigrantes/ 5) /((.$Pob.Total + .$Pob.5ymas) / 2)) * 1000,
Tasa.Mig = Tasa.Inmig - Tasa.Emig,
Eficacia = Mig.Neta - Mig.Bruta)
write.xlsx(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Intermetropolitano) 2010_Base2020.xlsx"), overwrite = TRUE)
save(tabla, file = paste0(here::here(), "/Output/Municipio/06_Zonas Metropolitanas/2010/01_Migracion reciente/Base 2020/Indicadores de MR5a por ZM 2010 (Intermetropolitano) 2010_Base2020.RData"))| Indicadores de migración reciente (Nivel intermetropolitano) | |||||||||||
| Zonas Metropolitanas | |||||||||||
| CVE_ZM | Pob.Total | Pob.5ymas | Residentes | Inmigrantes | Emigrantes | Mig.Neta | Mig.Bruta | Tasa.Inmig | Tasa.Emig | Tasa.Mig | Eficacia |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Fuente: Estimaciones del CONAPO. | |||||||||||
Librerias que se usaron en el documento
| package | loadedversion | source |
|---|---|---|
| Cairo | 1.6-1 | CRAN (R 4.3.1) |
| chorddiag | 0.1.3 | Github (mattflor/chorddiag@1688d72cd93071abb373e054190363bdfb3af2af) |
| circlize | 0.4.15 | CRAN (R 4.3.1) |
| doMC | 1.3.5 | R-Forge (R 4.3.1) |
| dplyr | 1.1.3 | CRAN (R 4.3.2) |
| expss | 0.11.6 | CRAN (R 4.3.1) |
| foreach | 1.5.2 | CRAN (R 4.3.1) |
| ggalluvial | 0.12.5 | CRAN (R 4.3.1) |
| ggplot2 | 3.4.3 | CRAN (R 4.3.1) |
| ggpubr | 0.6.0 | CRAN (R 4.3.1) |
| ggrepel | 0.9.3 | CRAN (R 4.3.1) |
| ggsankey | 0.0.99999 | Github (davidsjoberg/ggsankey@3e171a83a5364bb24df7cb2cd9203dd79b1dae29) |
| gt | 0.10.0 | CRAN (R 4.3.1) |
| haven | 2.5.3 | CRAN (R 4.3.1) |
| Hmisc | 5.1-0 | CRAN (R 4.3.1) |
| iterators | 1.0.14 | CRAN (R 4.3.1) |
| janitor | 2.2.0 | CRAN (R 4.3.1) |
| kableExtra | 1.3.4 | CRAN (R 4.3.1) |
| knitr | 1.45 | CRAN (R 4.3.2) |
| maditr | 0.8.3 | CRAN (R 4.3.1) |
| Matrix | 1.6-1.1 | CRAN (R 4.3.1) |
| network | 1.18.1 | CRAN (R 4.3.1) |
| openxlsx | 4.2.5.2 | CRAN (R 4.3.1) |
| reshape2 | 1.4.4 | CRAN (R 4.3.1) |
| sjlabelled | 1.2.0 | CRAN (R 4.3.1) |
| sna | 2.7-1 | CRAN (R 4.3.1) |
| srvyr | 1.2.0 | CRAN (R 4.3.1) |
| statnet.common | 4.9.0 | CRAN (R 4.3.1) |
| stringr | 1.5.0 | CRAN (R 4.3.1) |
| survey | 4.2 | Github (bschneidr/fastsurvey@5e4df7bd6c4bac44fa9c6681db40c496dd701f45) |
| survival | 3.5-5 | CRAN (R 4.3.1) |
| tibble | 3.2.1 | CRAN (R 4.3.1) |
| tidyr | 1.3.1 | CRAN (R 4.3.3) |
This
work by Diana Villasana
Ocampo is licensed under a
Creative
Commons Attribution 4.0 International License.